package zdream.rockchronicle.core.profile;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import zdream.control.world.Ticker;
import zdream.rockchronicle.RockChronicle;
import zdream.control.graphics.IPainter;

import static zdream.rockchronicle.profiles.megaman.MegaManHealthMiscPainter.*;
import static zdream.rockchronicle.core.profile.ProfilePage.*;

/**
 * Level 进行时, 按暂停, 查看自己角色信息的界面.
 * 界面画在前景上面 (symbolPainter).
 * 
 * 另外, 在信息界面关闭时, 血条也由它来画
 * 
 * @author Zdream
 * @since 0.0.1
 * @date
 *   2021-02-24 (last modified)
 */
public class ProfilePainter implements IPainter {

	final ProfilePage profile;
	final HealthMiscPainter healthMiscPainter;

	/**
	 * 背景图片
	 */
	Texture statusPanelBg;
	SpriteBatch batch;

	public ProfilePainter(ProfilePage profile, HealthMiscPainter healthMiscPainter) {
		this.profile = profile;
		this.healthMiscPainter = healthMiscPainter;
		this.initTextures();
		this.batch = profile.app.graphics.uiBatch;
	}

	private void initTextures() {
		statusPanelBg = profile.app.assets.getTexture("res/misc/status_panel.png");
	}

	public void draw(Ticker ticker) {
		// 如果在暂停状态, 不显示血条等
		if (ticker.paused) {
			return;
		}

		float profileBgAlpha = 0;

		// p1 血条位置确定
		switch (profile.status) {
		case STATUS_OPEN:
			healthMiscPainter.setX(P1_HP_BAR_X_WHEN_PROFILE_OPEN);
			profileBgAlpha = 1;
			break;
		case STATUS_CLOSE:
			healthMiscPainter.setX(P1_HP_BAR_X);
			break;
		case STATUS_OPENING:
			healthMiscPainter.setX((P1_HP_BAR_X
					+ (P1_HP_BAR_X_WHEN_PROFILE_OPEN - P1_HP_BAR_X)
					* (float) profile.opDuration / OP_DURATION));
			profileBgAlpha = 1f * profile.opDuration / OP_DURATION;
			break;
		case STATUS_CLOSING:
			healthMiscPainter.setX((P1_HP_BAR_X_WHEN_PROFILE_OPEN
					+ (P1_HP_BAR_X - P1_HP_BAR_X_WHEN_PROFILE_OPEN)
					* (float) profile.opDuration / OP_DURATION));
			profileBgAlpha = 1 - 1f * profile.opDuration / OP_DURATION;
			break;

		default:
			break;
		}

		// TODO 界面, 如果有
		if (profileBgAlpha > 0) {
			batch.begin();
			batch.setColor(1, 1, 1, profileBgAlpha);
			batch.draw(statusPanelBg, 0, 0);
			batch.setColor(1, 1, 1, 1); // 恢复原来配置
			batch.end();
		}
	}

	@Override
	public int zIndex() {
		return 2;
	}

	@Override
	public void dispose() {
		// do-nothing
	}
}
